package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.ButterKnife;

import com.devspark.progressfragment.ProgressDialogFragment;
import com.offroader.core.IFrag;
import com.offroader.utils.ViewUtils;
import com.offroader.view.MySwipeRefreshLayout;
import com.offroader.view.MySwipeRefreshLayout.OnLoadListener;
import com.umeng.analytics.MobclickAgent;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.activity.MyToolBarActivity;
import com.viphuli.app.tool.bean.PageBaseBean;
import com.viphuli.app.tool.common.Constants;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler.HandlerType;

public abstract class BaseProgressDialogFragment extends ProgressDialogFragment implements IFrag<PageBaseBean>, OnRefreshListener, OnLoadListener {
	private boolean canRefresh = true;
	private boolean canLoadMore = true;
	private View mContentView;
	private Handler handler = new Handler();
	private Dialog waitDialog;

	protected MySwipeRefreshLayout swipeRefreshLayout;
	protected MyToolBarActivity caller;
	protected int page;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		if (mContentView == null) {

			//1. 获取布局文件资源布局文件资源
			int contentRes = obtainContentRes();
			mContentView = inflater.inflate(contentRes, null);
			
		}
		
		//bind
		ButterKnife.bind(this, mContentView);

		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onResume() {
		super.onResume();
		//友盟
		MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面
	}

	@Override
	public void onPause() {
		super.onPause();
		//友盟
		MobclickAgent.onPageEnd(getClass().getSimpleName());
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();

		//unbind
		ButterKnife.unbind(this);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		hideLoadDialog(true);//关闭加载

		if (getActivity() instanceof MyToolBarActivity)
			caller = (MyToolBarActivity) getActivity();

		//2. 解析参数
		initParams();

		//3.设置视图
		setContentView(mContentView);

		//4.设置空内容文本
		setEmptyText(R.string.empty);

		//5.初始化视图组件
		initView();

		initSwipeLayout();

		//6.初始化数据
		initData();

	}

	@Override
	public void onBackPressed() {
		if (!isAdded())
			return;

		getActivity().finish();
	}

	@Override
	public void onRequestResonse(PageBaseBean page) {
		// 供网络请求Handler回调
	}

	@Override
	public void setRefreshing(boolean isShow) {
		swipeRefreshLayout.setRefreshing(isShow);
	}

	@Override
	public void setMore(final boolean isShow) {

		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				swipeRefreshLayout.setLoading(isShow);

			}
		}, 500);

	}

	/**
	 * 下拉刷新
	 */
	@Override
	public void onRefresh() {
		initRequest(HandlerType.refresh);
	}

	/**
	 * 上拉加载更多
	 */
	@Override
	public void onLoadMore() {
		initRequest(HandlerType.more);
	}

	@Override
	public boolean isShowBack() {
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return false;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == Activity.RESULT_OK) {//成功

			//刷新
			if (requestCode == Constants.ACTIVITY_REQUEST_REFREASH_CODE) {
				initData();
			}

		}

	}

	public void showWaitDialog() {

		if (waitDialog == null)
			waitDialog = ViewUtils.progressWaitting(this);

		waitDialog.show();

	}

	public void hideWaitDialog() {

		if (waitDialog != null && waitDialog.isShowing())
			waitDialog.hide();

	}

	private void initSwipeLayout() {
		//下拉刷新
		swipeRefreshLayout = ButterKnife.findById(mContentView, R.id.id_swipe_layout);

		//刷新监听器
		if (swipeRefreshLayout != null) {

			//下拉刷新
			if (canRefresh)
				swipeRefreshLayout.setOnRefreshListener(this);

			//加载更多
			if (canLoadMore)
				swipeRefreshLayout.setOnLoadListener(this);

		}

	}

	public void showLoadDialog() {

		setContentShown(false);

	}

	public void hideLoadDialog() {

		doSetContentShown(true);

	}

	public void hideLoadDialog(boolean delay) {

		if (delay)

			handler.postDelayed(new Runnable() {

				@Override
				public void run() {
					hideLoadDialog();
				}
			}, 300);

		else {
			hideLoadDialog();
		}

	}

	private void doSetContentShown(boolean shown) {

		if (isAdded())
			setContentShown(shown);

	}

	public void empty() {
		setContentEmpty(true);
	}

	public void notEmpty() {
		setContentEmpty(false);
	}

	public boolean isEmpty() {
		return isContentEmpty();
	}

	/**
	 * 无数据
	 */
	public void doShowEmpty() {
		setEmptyText(R.string.empty);
		empty();

		ImageView emptyImg = getEmptyImg();
		if (emptyImg != null)
			emptyImg.setImageResource(R.drawable.ic_empty);

		View emptyBtn = getEmptyBtn();
		if (emptyBtn != null) {
			emptyBtn.setVisibility(View.GONE);
		}
	}

	/**
	 * 网络错
	 */
	@Override
	public void doShowNetError() {
		setEmptyText(R.string.net_error);
		empty();

		ImageView emptyImg = getEmptyImg();
		if (emptyImg != null)
			emptyImg.setImageResource(R.drawable.ic_error_net);

		TextView emptyBtn = (TextView) getEmptyBtn();
		if (emptyBtn != null) {
			emptyBtn.setText(getString(R.string.btn_page_reload));
			emptyBtn.setVisibility(View.VISIBLE);
			emptyBtn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					//重新加载
					initRequest(HandlerType.loading);
				}
			});
		}

	}

	/**
	 * 未登陆
	 */
	public void doShowLogin() {
		setEmptyText(R.string.tip_login);//文字提示
		empty();

		ImageView emptyImg = getEmptyImg();
		if (emptyImg != null)
			emptyImg.setImageResource(R.drawable.ic_no_login);//图片提示

		TextView emptyBtn = (TextView) getEmptyBtn();
		if (emptyBtn != null) {
			emptyBtn.setText(getString(R.string.btn_page_login));
			emptyBtn.setVisibility(View.VISIBLE);
			emptyBtn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					//去登陆
					AccountLoginFragment.go(getActivity(), Constants.ACTIVITY_REQUEST_REFREASH_CODE);
				}
			});
		}

	}

	/**
	 * 隐藏错误提示
	 */

	public boolean isShownNetError() {

		TextView emptyBtn = (TextView) getEmptyBtn();
		if (emptyBtn != null && emptyBtn.getVisibility() == View.VISIBLE && getString(R.string.btn_page_reload).equals(emptyBtn.getText().toString()))
			return true;

		return false;

	}

	/**
	 * 获取布局文件资源
	 */
	protected abstract int obtainContentRes();

	/**
	 * 初始化参数
	 */
	protected abstract void initParams();

	/**
	 * 初始化视图组件
	 */
	protected abstract void initView();

	/**
	 * 初始化数据
	 */
	protected abstract void initData();

	/**
	 * 获取数据(下拉刷新须实现)
	 */
	protected void initRequest(HandlerType handlerType) {
	}

	public void setCanRefresh(boolean canRefresh) {
		this.canRefresh = canRefresh;
	}

	public void setCanLoadMore(boolean canLoadMore) {
		this.canLoadMore = canLoadMore;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
}
